﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class AnswerSys : MonoBehaviour
{
    public static AnswerSys Instance = null;

    public string[] Answer;
    [SerializeField]
    private string[] userAnswer;

   
    public string currentAnswer;


    public int currentQuesitionIndex;

    public Transform QuesitionParent;

    public GameObject ResultPanel;
    public Text errorQuesitionTip;
    public Button SubmitBtn;
    public Button ReLoadBtn;
    public Text ScoreTxt;

    private void Awake()
    {

        Instance = this;
        userAnswer = new string[Answer.Length];

    }
    public void SubmitAnswer()
    {
        if (currentAnswer == "") return;
        


        //答案排序
        string[] strs = currentAnswer.Split('/');
        if (strs.Length > 1)
        {
            Debug.Log(strs.Length);
            int[] values = new int[strs.Length];
            for (int j = 0; j < strs.Length; j++)
            {

                if (strs[j] != "")
                {

                    values[j] = int.Parse(strs[j]);
                }

            }
            values = MTM(values);

            currentAnswer = "";
            for (int k = 0; k < values.Length; k++)
            {
                if (values[k] != 0)
                {

                    currentAnswer += values[k];
                }
            }
        }
       
       
        Debug.Log(currentAnswer);
        userAnswer[currentQuesitionIndex] = currentAnswer;
        currentAnswer = "";
        NextQuesition();
    }


    public void NextQuesition()
    {
        if (currentQuesitionIndex < QuesitionParent.childCount - 1)
        {
            currentQuesitionIndex++;

        }
        else
        {
            string indexs = "";
            int errorIndex = 0;
            //答题核算
            for (int i = 0; i < Answer.Length; i++)
            {
                if (userAnswer[i] != Answer[i])
                {
                    indexs += i+1;
                    errorIndex++;
                    if (i < Answer.Length-1)
                    {
                        indexs += "、";

                    }
                }
            }
            Debug.Log("您有以下几道题的解答有误：" + indexs);
            float sinleScore = 100 / Answer.Length;
            float score = 100 - (sinleScore * errorIndex);
            ScoreTxt.text = "您的答题总得分为:" + score.ToString("0.0");
            ResultPanel.SetActive(true);
            errorQuesitionTip.text = indexs;
            SubmitBtn.gameObject.SetActive(false);
            ReLoadBtn.gameObject.SetActive(true);
            QuesitionParent.gameObject.SetActive(false);
            return;
        }
        SetCurrentQuesition(currentQuesitionIndex, QuesitionParent);

    }


    public void SetCurrentQuesition(int index, Transform parent)
    {
        if (index >= parent.childCount) return;
        for (int i = 0; i < parent.childCount; i++)
        {
            parent.GetChild(i).gameObject.SetActive(false);
        }

        parent.GetChild(index).gameObject.SetActive(true);
    }

    public void RemoveAnswer(string answer)
    {
        
        if (currentAnswer.Contains(answer))
        {
            
            currentAnswer=currentAnswer.Replace(answer+"/", "");
           
        }
    }


    public int[] MTM(int[] array)
    {
        int[] FianlMTM = new int[array.Length];

        for (int j = 0; j < array.Length; j++)
        {
            int last = 0;
            for (int i = 1; i < array.Length - j; i++)
            {
                int t = array[i];

                if ((array[i]) < (array[i - 1]))
                {

                    array[i] = array[i - 1];
                    last = array[i];
                    array[i - 1] = t;

                }
                else
                    last = array[i];

            }
            FianlMTM[array.Length - (j + 1)] = last;

        }
        return FianlMTM;

    }
}
